<!-- by 7527 18920 -->
<template>
  <a-row :gutter="24">
    <a-col :md="24">
      <a-card :style="cardStyle" :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <!-- 搜索区域 -->
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('search.chequeNumber')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-input :placeholder="$t('placeholder.pleaseInputChequeNumber')"
                           v-model="queryParam.chequeNumber"></a-input>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('search.chequeDateRange')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-range-picker
                    style="width:100%"
                    v-model="queryParam.createTimeRange"
                    format="YYYY-MM-DD"
                    :placeholder="[$t('placeholder.pleaseInputStartTime'), $t('placeholder.pleaseInputEndTime')]"
                    @change="onDateChange"
                    @ok="onDateOk"
                  />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('search.chequeStatus')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select :placeholder="$t('placeholder.pleaseInputChequeStatus')" v-model="queryParam.status">
                    <a-select-option value="0">{{ $t('status.unRealized') }}</a-select-option>
                    <a-select-option value="1">{{ $t('status.realized') }}</a-select-option>
                    <a-select-option value="2">{{ $t('status.checqueReturn') }}</a-select-option>
                    <a-select-option value="3">{{ $t('status.returnToCustomer') }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>


              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('search.operator')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select :placeholder="$t('placeholder.pleaseSelectOperator')" showSearch
                            optionFilterProp="children"
                            v-model="queryParam.creator">
                    <a-select-option v-for="(item,index) in userList" :key="index" :value="item.id">
                      {{ item.userName }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('search.salesMan')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select :placeholder="$t('placeholder.pleaseSelectSalesMan')" showSearch
                            optionFilterProp="children"
                            v-model="queryParam.handsPersonId">
                    <a-select-option v-for="(item,index) in personList" :key="index" :value="item.id">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('search.accountName')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select :placeholder="$t('placeholder.pleaseSelectAccountName')" showSearch
                            optionFilterProp="children"
                            v-model="queryParam.accountId">
                    <a-select-option v-for="(item,index) in accountList" :key="index" :value="item.id">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('search.customer')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select :placeholder="$t('placeholder.pleaseSelectCustomer')" showSearch
                            optionFilterProp="children"
                            v-model="queryParam.organId">
                    <a-select-option v-for="(item,index) in cusList" :key="index" :value="item.id">
                      {{ item.supplier }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-button type="primary" @click="searchQuery">{{ $t('button.search') }}</a-button>
              <a-button style="margin-left: 8px" @click="searchReset">{{ $t('button.reset') }}</a-button>
              <!--                <a-col :md="6" :sm="24">-->
              <!--                  <a-form-item label="销售单号" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
              <!--                    <a-input placeholder="请输入销售单号" v-model="queryParam.number"></a-input>-->
              <!--                  </a-form-item>-->
              <!--                </a-col>-->

            </a-row>
          </a-form>
        </div>
        <!-- 操作按钮区域 -->
        <div class="table-operator" style="margin-top: 5px">
          <a-button v-if="btnEnableList.indexOf(1)>-1" @click="myHandleAdd" type="primary" icon="plus">
            {{ $t('button.add') }}
          </a-button>
          <a-button v-if=" btnEnableList.indexOf(2)>-1" icon="check" @click="approveSetStatus(2)">
            {{ $t('button.approve') }}
          </a-button>
          <a-button v-if=" btnEnableList.indexOf(7)>-1" icon="stop" @click="approveSetStatus(0)">
            {{ $t('button.backCheck') }}
          </a-button>
          <a-button v-if="btnEnableList.indexOf(1)>-1" @click="chequeBankName">{{ $t('button.chequeBankName') }}
          </a-button>
          <a-button v-if=" btnEnableList.indexOf(2)>-1" @click="payeeName">{{ $t('button.payeeName') }}
          </a-button>
          <a-tooltip placement="left" :title="this.$t('title.chequeListTitle')" slot="action">
            <a-icon v-if="btnEnableList.indexOf(1)>-1" type="question-circle" style="font-size:20px;float:right;" />
          </a-tooltip>
        </div>
        <!-- table区域-begin -->
        <div>
          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :components="handleDrag(columns)"
            :pagination="ipagination"
            :scroll="scroll"
            :loading="loading"
            :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
            @change="handleTableChange">
            <span slot="action" slot-scope="text, record">
              <a @click="myHandleDetail(record, '支票', prefixNo)">{{ $t('button.view') }}</a>
              <a-divider v-if="btnEnableList.indexOf(1)>-1" type="vertical" />
              <a v-if="btnEnableList.indexOf(1)>-1" @click="chequeEdit(record)">{{ $t('button.edit') }}</a>
              <a-divider v-if="btnEnableList.indexOf(1)>-1" type="vertical" />
              <a-popconfirm v-if="btnEnableList.indexOf(1)>-1" :title="$t('title.confirmDelete')"
                            @confirm="() => handleChequeDelete(record)">
                <a>{{ $t('button.delete') }}</a>
              </a-popconfirm>
            </span>
            <template slot="customRenderStatus" slot-scope="status">
              <a-tag v-if="status == 0" color="orange">{{ $t('status.unRealized') }}</a-tag>
              <a-tag v-if="status == 1" color="blue">{{ $t('status.realized') }}</a-tag>
              <a-tag v-if="status == 2" color="red">{{ $t('status.checqueReturn') }}</a-tag>
              <a-tag v-if="status == 3" color="red">{{ $t('status.returnToCustomer') }}</a-tag>
            </template>
            <template slot="approveStatus" slot-scope="status">
              <a-tag v-if="status == 0" color="blue">{{ $t('status.initApproved') }}</a-tag>
              <a-tag v-if="status == 1" color="orange">{{ $t('status.toApproved') }}</a-tag>
              <a-tag v-if="status == 2" color="green">{{ $t('status.approved') }}</a-tag>
            </template>
          </a-table>
        </div>
        <!-- table区域-end -->
        <!-- 表单区域 -->
        <cheque-modal ref="modalForm" @ok="modalFormOk" @close="modalFormClose"></cheque-modal>
        <cheque-detail-modal ref="modalDetail" @ok="modalFormOk" @close="modalFormClose"></cheque-detail-modal>
        <cheque-bank-name-modal ref="bankNameModal"></cheque-bank-name-modal>
        <cheque-payee-name-modal ref="payeeNameModal"></cheque-payee-name-modal>
      </a-card>
    </a-col>
  </a-row>
</template>
<script>
import ChequeModal from './modules/ChequeModal'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import {FinancialListMixin} from './mixins/FinancialListMixin'
import JDate from '@/components/jeecg/JDate'
import ChequeDetailModal from '@views/financial/modules/ChequeDetailModal.vue'
import {deleteAction, postAction} from '@api/manage'
import {getPersonByType,} from '@/api/api'
import Vue from 'vue'
import {USER_IS_ADMIN} from '@/store/mutation-types'
import ChequeBankNameModal from './dialog/ChequeBankNameModal'
import ChequePayeeNameModal from './dialog/ChequePayeeNameModal'

export default {
  name: 'ChequeList',
  mixins: [JeecgListMixin, FinancialListMixin],
  components: {
    ChequeModal,
    ChequeDetailModal,
    JDate,
    ChequeBankNameModal,
    ChequePayeeNameModal
  },
  data () {
    return {
      labelCol: {
        span: 5
      },
      wrapperCol: {
        span: 18,
        offset: 1
      },
      // 查询条件
      queryParam: {
        chequeNumber: '',
        issueDate: null,
        payDate: null,
        payeeName: '',
        returnReason: '',
        bankName: '',
        supplierId: '',
        creator: '',
        handsPersonId: '',
        accountId: '',
        status: null,
        remark: ''

      },
      prefixNo: 'CL',
      // 表头
      columns: [
        {
          title: this.$t('dynamic.operation'),
          dataIndex: 'action',
          width: 140,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        },
        {title: this.$t('search.chequeSerialNo'), dataIndex: 'serialNo', width: 80, ellipsis: true},
        {title: this.$t('search.customer'), dataIndex: 'supplier', width: 90, ellipsis: true},
        {title: this.$t('search.chequeNumber'), dataIndex: 'chequeNumber', width: 120},
        {title: this.$t('search.chequeAmount'), dataIndex: 'amount', width: 110},
        {title: this.$t('search.outBillNo'), dataIndex: 'ckNo', width: 110},
        {title: this.$t('search.saleBillNo'), dataIndex: 'xsNo', width: 130},
        {title: this.$t('search.operator'), dataIndex: 'userName', width: 90, ellipsis: true},
        { title: this.$t('search.financialStaff'), dataIndex: 'handsPersonName', width: 80 },
        { title: this.$t('search.chequePayDate'), dataIndex: 'payDate', width: 80 },
        { title: this.$t('search.chequeDateRange'), dataIndex: 'createTimeStr', width: 80 },
        {
          title: this.$t('search.status'), dataIndex: 'status', width: 80, align: 'center',
          scopedSlots: { customRender: 'customRenderStatus' }
        },
        {
          title: this.$t('search.chequeApproveStatus'), dataIndex: 'approveStatus', width: 80, align: 'center',
          scopedSlots: {customRender: 'approveStatus'}
        }
      ],
      url: {
        list: '/cheque/list',
        delete: '/cheque/delete',
        deleteBatch: '/cheque/deleteBatch',
        batchSetStatusUrl: '/cheque/batchSetStatus'
      }
    }
  },
  computed: {},
  created () {
    // this.initSystemConfig()
    this.initCustomer()
    this.initUser()
    this.initSalesman()
    this.initAccount()
  },
  methods: {
    chequeEdit(record) {
      //不是已兑现且未审核通过的状态才能编辑
      if (record.approveStatus !== 2 || Vue.ls.get(USER_IS_ADMIN)) {
        this.$refs.modalForm.action = 'edit'
        if (this.btnEnableList.indexOf(2) === -1) {
          this.$refs.modalForm.isCanCheck = false
        }

        this.handleEdit(record)
      } else {
        this.$message.warning(this.$t('message.deleteUncheckedChequeOnly'))
      }
    },
    initSalesman() {
      let that = this
      getPersonByType({type: '销售员'}).then((res) => {
        if (res && res.code === 200) {
          that.personList = res.data.personList
        }
      })
    },
    handleChequeDelete(record) {
      if (record.status === 0 || Vue.ls.get(USER_IS_ADMIN)) {
        if (!this.url.delete) {
          this.$message.error('请设置url.delete属性!')
          return
        }
        var that = this
        deleteAction(that.url.delete, {id: record.id}).then((res) => {
          if (res.code === 200) {
            that.loadData(1)
          } else {
            that.$message.warning(res.data.message)
          }
        })
      } else {
        this.$message.warning(this.$t('message.deleteUncheckedChequeOnly'))
      }
    },
    chequeBankName() {
      this.$refs.bankNameModal.show()
    },
    payeeName() {
      this.$refs.payeeNameModal.show()
    },
    approveSetStatus: function (status) {

      if (!this.url.batchSetStatusUrl) {
        this.$message.error('请设置url.batchSetStatusUrl属性!')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning(this.$t('message.pleaseSelectARecord'))
        return
      } else {

        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        var that = this

        this.$confirm({
          title: this.$t('dynamic.queren'),
          content: this.$t('dynamic.pleaseConfirmOperation'),
          onOk: function () {

            that.loading = true
            postAction(that.url.batchSetStatusUrl, {
              status: status,
              ids: ids,
              subType: that.queryParam.subType
            }).then((res) => {

              if (res.code === 200) {
                that.loadData()
                that.onClearSelected()
                that.$message.info(this.$t('message.approveSuccess'))
              } else {
                that.$message.warning(res.data.message)
              }
            }).finally(() => {
              that.loading = false
            })
          }
        })
      }
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>